package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralControllerAlarmRecord;
import com.ruoyi.central.service.ITopCentralControllerAlarmRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Collection;
/**
 * 报警记录Controller
 * 
 * @author ruoyi
 * @date 2022-02-25
 */
@Controller
@RequestMapping("/central/centralControllerAlarmRecord")
public class TopCentralControllerAlarmRecordController extends BaseController
{
    private String prefix = "central/centralControllerAlarmRecord";

    @Autowired
    private ITopCentralControllerAlarmRecordService topCentralControllerAlarmRecordService;

    @RequiresPermissions("central:centralControllerAlarmRecord:view")
    @GetMapping()
    public String centralControllerAlarmRecord()
    {
        return prefix + "/centralControllerAlarmRecord";
    }

    /**
     * 查询报警记录列表
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralControllerAlarmRecord topCentralControllerAlarmRecord)
    {
        startPage();
        List<TopCentralControllerAlarmRecord> list = topCentralControllerAlarmRecordService.selectList(topCentralControllerAlarmRecord);
        return getDataTable(list);
    }

    /**
     * 查询简单的报警记录列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralControllerAlarmRecord topCentralControllerAlarmRecord){

        Collection<TopCentralControllerAlarmRecord> topCentralControllerAlarmRecordList = topCentralControllerAlarmRecordService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralControllerAlarmRecordList,TopCentralControllerAlarmRecord::getId,TopCentralControllerAlarmRecord::getDeviceId));
    }

    /**
     * 获取报警记录详细信息
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralControllerAlarmRecordService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralControllerAlarmRecord 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralControllerAlarmRecord topCentralControllerAlarmRecord){
        int weigth = topCentralControllerAlarmRecordService.getMaxOfWeight(topCentralControllerAlarmRecord);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载报警记录导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralControllerAlarmRecord> util = new ExcelUtil<>(TopCentralControllerAlarmRecord.class);
        util.importTemplateExcel(response, "报警记录数据");
    }

    /**
     * 导入报警记录数据
     * @param file 报警记录数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "报警记录", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralControllerAlarmRecord:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralControllerAlarmRecord> util = new ExcelUtil<>(TopCentralControllerAlarmRecord.class);
        List<TopCentralControllerAlarmRecord> topCentralControllerAlarmRecordList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralControllerAlarmRecordService.updateBatchById(topCentralControllerAlarmRecordList)){
                return AjaxResult.success("导入报警记录数据覆盖成功！");
            }
            return AjaxResult.error("导入报警记录数据覆盖失败！");
        }
        if(topCentralControllerAlarmRecordService.insertBatch(topCentralControllerAlarmRecordList)){
            return AjaxResult.success("导入报警记录数据新增成功！");
        }
        return AjaxResult.error("导入报警记录数据新增失败！");
    }

    /**
     * 导出报警记录列表
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:export")
    @Log(title = "报警记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralControllerAlarmRecord topCentralControllerAlarmRecord)
    {
        List<TopCentralControllerAlarmRecord> list = topCentralControllerAlarmRecordService.selectList(topCentralControllerAlarmRecord);
        ExcelUtil<TopCentralControllerAlarmRecord> util = new ExcelUtil<TopCentralControllerAlarmRecord>(TopCentralControllerAlarmRecord.class);
        return util.exportExcel(list, "报警记录数据");
    }

    /**
     * 新增报警记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报警记录
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:add")
    @Log(title = "报警记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralControllerAlarmRecord topCentralControllerAlarmRecord)
    {
        return toAjax(topCentralControllerAlarmRecordService.insert(topCentralControllerAlarmRecord));
    }

    /**
     * 修改报警记录
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralControllerAlarmRecord topCentralControllerAlarmRecord = topCentralControllerAlarmRecordService.selectById(id);
        mmap.put("topCentralControllerAlarmRecord", topCentralControllerAlarmRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存报警记录
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:edit")
    @Log(title = "报警记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralControllerAlarmRecord topCentralControllerAlarmRecord)
    {
        return toAjax(topCentralControllerAlarmRecordService.updateById(topCentralControllerAlarmRecord));
    }

    /**
     * 恢复报警记录 为未删除 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:restore")
    @Log(title = "报警记录", businessType = BusinessType.UPDATE)
    @PutMapping("/restoreData/{ids}")
    @ResponseBody
    public AjaxResult recoverByIds(@PathVariable("ids") List<Integer> ids){
        return toAjax(topCentralControllerAlarmRecordService.recoverBatchByIds(ids));
    }

    /**
     * 删除报警记录
     */
    @RequiresPermissions("central:centralControllerAlarmRecord:remove")
    @Log(title = "报警记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralControllerAlarmRecordService.logicDeleteBatchByIds(ids));
    }
}
